<template>
  <div>
    <el-input v-model="bname" placeholder="请输入商品类别" style="width:170px;"></el-input>
    <el-input v-model="name" placeholder="请输入商品编码或者名称" style="width:170px;"></el-input>
    <el-button type="success" @click="exportData" icon="el-icon-download">导出数据</el-button>
    <el-upload
      :show-file-list="false"
      :before-upload="beforeUpload"
      :on-success="onSuccess"
      :on-error="onError"
      :disabled="importDataDisabled"
      style="display: inline-flex;margin-right: 8px"
      :action="action"
    >
      <el-button
        :disabled="importDataDisabled"
        type="success"
        :icon="importDataBtnIcon"
      >{{importDataBtnText}}</el-button>
    </el-upload>
    <el-button type="success" @click="getList()">查询</el-button>
    <!-- 二维码 -->
    <canvas id="q"></canvas>
    <!-- 库存列表 -->
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column fixed prop="code" label="商品编码" width="150"></el-table-column>
      <el-table-column prop="name" label="姓名" width="150"></el-table-column>
      <el-table-column prop="bname" label="类别" width="150"></el-table-column>
      <el-table-column prop="model" label="型号" width="150"></el-table-column>
      <el-table-column prop="minNum" label="库存总量" width="150"></el-table-column>
      <el-table-column prop="inventoryQuantity" label="销售数" width="150"></el-table-column>
      <el-table-column prop="lastPurchasingPrice" label="上次进价" width="150"></el-table-column>
      <el-table-column prop="purchasingPrice" label="平均售价" width="150"></el-table-column>
      <el-table-column prop="sellingPrice" label="销售价" width="150"></el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNo"
      :page-sizes="[10, 20, 30]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
  </div>
</template>

<script>
import axios from "axios";
export default {
  methods: {
    onSuccess(response, file, fileList) {
      alert("导入成功");
      console.log("导入成功:{}", response);
      this.importDataBtnText = "导入数据";
      this.importDataBtnIcon = "el-icon-upload2";
      this.importDataDisabled = false;
      this.getList();
    },
    onError(response, file, fileList) {
      console.log("error----------------->:{}", response);
      alert(response.message);
      this.importDataBtnText = "导入数据";
      this.importDataBtnIcon = "el-icon-upload2";
      this.importDataDisabled = false;
    },
    beforeUpload(response, file, fileList) {
      this.importDataBtnText = "正在导入";
      this.importDataBtnIcon = "el-icon-loading";
      this.importDataDisabled = true;
    },
    exportData() {
      var url =
        "http://localhost:9999/biz/tGoods/downLoadGoodsExcel?pageNo={pageNo}&pageSize={pageSize}&access_token={token}";
      url = url.replace("{pageNo}", this.pageNo);
      url = url.replace("{pageSize}", this.pageSize);
      url = url.replace("{token}", localStorage.getItem("access_token"));
      if (this.name) {
        url = url + "&name=" + this.name;
      }
      console.log("url:----------------------", url);
      window.open(url, "_parent");
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNo = val;
      this.getList();
    },
    getList() {
      var $this = this;
      this.axios({
        method: "post",
        url: "http://localhost:9999/biz/tGoods/getGoodsList",
        params: {
          pageNo: this.pageNo,
          pageSize: this.pageSize,
          bname: this.bname,
          name: this.name,
          access_token: localStorage.getItem("access_token")
        }
      }).then(function(res) {
        $this.tableData = res.data.obj.list;
        $this.total = res.data.obj.total;
      });
    }
  },
  data() {
    return {
      tableData: [],
      pageNo: 1,
      pageSize: 10,
      total: 10,
      name: "",
      bname: "",
      importDataDisabled: false,
      importDataBtnText: "导入数据",
      importDataBtnIcon: "el-icon-upload2",
      action: ""
    };
  },
  mounted() {
    this.getList();
    this.action =
      "http://localhost:9999/biz/tGoods/upLoadExcel?access_token=" +
      localStorage.getItem("access_token");
    var canvas = document.getElementById("q");
    var $this = this;
    this.axios({
      method: "post",
      url: "http://localhost:9999/biz/alibaba/pay/createPayOrder",
      params: {
        access_token: localStorage.getItem("access_token"),
        orderNo: new Date().getTime()
      }
    }).then(function(res) {
        console.log("生成支付宝扫码二维码返回信息为{}",res);
       $this.QRCode.toCanvas(
        canvas,
        res.data.obj,
        function(error) {
          if (error) {
            console.error(error);
          }
          console.log("QRCode success!");
        }
      );
    });
  }
};
</script>